#include "pdfhelper.h"
#include <QTextStream>
#include <QMap>
#include <QDateTime>
#include <QVariant>
#include <QCryptographicHash>

PdfHelper::PdfHelper()
{
}

QString PdfHelper::generateFDF(QObject* model)
{
    QString output;
    QTextStream stream(&output);
    QCryptographicHash md5(QCryptographicHash::Md5);
    md5.addData(qPrintable(QDateTime::currentDateTime().toString()), QDateTime::currentDateTime().toString().length());

    stream << "%FDF-1.2\n%????\n1 0 obj \n<< /FDF << /Fields [ ";

    // TODO
    QMap<QString, QString> map;
    map["Character"]    = "CharacterName";
    map["Occupation"]           = "Occupation";
    map["Drive"]                = "Drive";
    map["Occupational benefits"]  = "OccupationBenefits";
    map["Pillars of Sanity"]    = "Pillars";

    foreach(QString key, map.keys())
        stream << "<</T(" << key << ")/V("
               << model->property(qPrintable(map[key])).toString().trimmed()
               << ")>>";

    stream << "] \n/F (dummy.pdf) /ID [ <" << md5.result().toHex() << ">\n] >>";
    stream << " \n>> \nendobj\ntrailer\n";
    stream << "<<\n/Root 1 0 R \n\n>>\n%%EOF\n";

    return output;
}
